package k2_oop.p9_collection.demo01;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class UseStream {

    /**
     * 通过已知集合转成Stream流
     * @TODO 这里可以把参数和返回值的泛型改成 E
     * @param collection
     * @return
     */
    public Stream<Integer> createStreamByCollection(Collection<Integer> collection){
        return collection.stream();
    }

    /**
     * 通过不定长个参数创建Stream流
     * @TODO 这里可以把参数和返回值的泛型改成 E
     * @param num
     * @return
     */
    public Stream<Integer> createStreamByElements(Integer... num){
        return Stream.of(num);
    }

    public void useFinallyMethod(Stream<Integer> stream){
        //        System.out.println(numStream.count());

//        numStream1.forEach(System.out::print);

//        System.out.println(numStream.collect(Collectors.toSet()));

//        boolean boo = numStream.allMatch(i -> i%2==0);
//        System.out.println(boo);

//        System.out.println(numStream.anyMatch(i -> i % 2 == 0));

//        System.out.println(numStream.noneMatch(i -> i % 2 == 0));

//        System.out.println(numStream.findFirst());
//        System.out.println(numStream.findAny());

//        System.out.println(animals.max((s1, s2) -> {
//
//            int n1 = Integer.parseInt(s1.substring(s1.lastIndexOf("-")+1));
//            int n2 = Integer.parseInt(s2.substring(s2.lastIndexOf("-")+1));
//
//            int num1 = Integer.parseInt(s1.split("-")[1]);
//            int num2 = Integer.parseInt(s2.split("-")[1]);
//            return n1-n2;
//        }));

        System.out.println(stream.reduce(Integer::sum));
    }

    public void useNotFinallyMethod(Stream<Integer> stream){

//        stream
//                .filter(i->i%3==0)
//                .forEach(System.out::println);

//        stream
//                .limit(4)
//                .filter(i->i%3==0)
//                .forEach(System.out::println);

//        stream
//                .skip(4)
//                .filter(i->i%3==0)
//                .forEach(System.out::println);

//        stream
//                .map(i->Math.pow(i,2))
//                .forEach(System.out::println);


        Stream<String> animals = Stream.of("panda-2","monkey-1","dog-9");

//        animals
//                .map(a->a.split("-"))
//                .map(s->s[0]+"======="+s[1])
//                .forEach(System.out::println);


        animals.flatMap(str->Stream.of(str.split("-"))).forEach(System.out::println);


        Stream<String> animals1 = Stream.of("cat-10","rabbit-25","tiger-3");

//        Stream.concat(animals,animals1)
//                .forEach(System.out::println);

//        animals.sorted().forEach(System.out::println);
//        animals.sorted((s1, s2) -> {
//
//            int n1 = Integer.parseInt(s1.substring(s1.lastIndexOf("-")+1));
//            int n2 = Integer.parseInt(s2.substring(s2.lastIndexOf("-")+1));
//
//            int num1 = Integer.parseInt(s1.split("-")[1]);
//            int num2 = Integer.parseInt(s2.split("-")[1]);
//            return n2-n1;
//        }).forEach(System.out::println);


//        stream.distinct().forEach(System.out::println);
    }

    public static void main(String[] args) {
        UseStream stream = new UseStream();
        ArrayList<Integer> nums = new ArrayList<>();
        nums.add(1);
        nums.add(2);
        nums.add(6);
        nums.add(4);
        nums.add(1);
        nums.add(2);
        //通过两种形式创建Stream
        Stream<Integer> numStream = stream.createStreamByCollection(nums);
        Stream<Integer> numStream1 = stream.createStreamByElements(6,7,8,9,10);

        stream.useNotFinallyMethod(numStream);

    }

}
